/*******************************************************************************
				TABLE 2
*******************************************************************************/

*WAGE IV, COWORKERS
*TABLE 2, PANEL A
cap{
use "$data/base WAGE IV companies.dta", clear

global xi edad edad2 sexo anos_educ puntaje lnw
global xg m_l_edad m_l_sexo m_l_puntaje m_l_anos_educ

capture confirm new variable rmiss
if _rc==110 drop rmiss
egen rmiss = rowmiss($xi $xg)
keep if rmiss==0

local dep l_net
local iv z_lnet

********************************************************************************
*ROW 1. MODEL WITHOUT COVARIATES:
eststo clear

*1. FIRST STAGE:
qui reg `dep' `iv', cluster(rut_emp)
	eststo first
	
*2. REDUCED FORM:
qui reg sej2012 `iv', cluster(rut_emp)
	eststo rf	
	
*3. SECOND STAGE:
qui ivreg sej2012 (`dep' = `iv'), cluster(rut_emp)
	eststo iv
}
*ROW 1. MODEL WITHOUT COVARIATES:
esttab first rf iv, keep(`dep' `iv') ///
	mtitles("First Stage" "Reduced Form" "TSLS") ///
	b(%6.4fc) se(%6.4fc) star(* 0.1 ** 0.05 *** 0.01) ///
	stats(N, fmt(%8.0fc) labels(Observations))  brackets ///
	coeflabels(l_net "Endogenous" z_lnet "Instrument")

cap{
********************************************************************************
*ROW 2. INDIVIDUAL COVARIATES:
eststo clear

*1. FIRST STAGE:
qui reg `dep' $xi `iv', cluster(rut_emp)
	eststo first_lnet

*2. REDUCED FORM:
qui reg sej2012 $xi `iv', cluster(rut_emp)
	eststo rf_zlnet		
	
*3. SECOND STAGE:
qui ivreg sej2012 $xi (`dep' = `iv'), cluster(rut_emp)
	eststo iv_lnet
}
*ROW 2. INDIVIDUAL COVARIATES:
esttab first_lnet rf_zlnet iv_lnet, keep(`dep' `iv') ///
	mtitles("First Stage" "Reduced Form" "TSLS") ///
	b(%6.4fc) se(%6.4fc) star(* 0.1 ** 0.05 *** 0.01) ///
	stats(N, fmt(%8.0fc) labels(Observations))  brackets ///
	coeflabels(l_net "Endogenous" z_lnet "Instrument")
	
cap{
********************************************************************************
*ROW 3. INDIVIDUAL & GROUP COVARIATES (BASELINE):
eststo clear

*1. FIRST STAGE:
qui reg `dep' $xi $xg `iv', cluster(rut_emp)
	eststo first_lnet

*2. REDUCED FORM:
qui reg sej2012 $xi $xg `iv', cluster(rut_emp)
	eststo rf_zlnet		
	
*3. SECOND STAGE:
qui ivreg sej2012 $xi $xg (`dep' = `iv'), cluster(rut_emp)
	eststo iv_lnet
}
*ROW 3. INDIVIDUAL & GROUP COVARIATES (BASELINE):
esttab first_lnet rf_zlnet iv_lnet, keep(`dep' `iv') ///
	mtitles("First Stage" "Reduced Form" "TSLS") ///
	b(%6.4fc) se(%6.4fc) star(* 0.1 ** 0.05 *** 0.01) ///
	stats(N, fmt(%8.0fc) labels(Observations))  brackets ///
	coeflabels(l_net "Endogenous" z_lnet "Instrument")
clear

********************************************************************************
********************************************************************************
*WAGE IV, CLASSMATES
*TABLE 2, PANEL A
cap{
use "$data/base WAGE IV schools.dta", clear

global xi edad edad2 sexo anos_educ puntaje lnw
global xg m_s_edad m_s_sexo m_s_puntaje m_s_anos_educ 

capture confirm new variable rmiss
if _rc==110 drop rmiss
egen rmiss = rowmiss($xi $xg)
keep if rmiss==0

local dep s_net
local iv z_snet

********************************************************************************
*ROW 1. MODEL WITHOUT COVARIATES:
eststo clear

*1. FIRST STAGE:
qui reg `dep' `iv', cluster(rbd)
	eststo first
	
*2. REDUCED FORM:
qui reg sej2012 `iv', cluster(rbd)
	eststo rf	

*3. SECOND STAGE:
qui ivreg sej2012 (`dep' = `iv'), cluster(rbd)
	eststo iv
}
*ROW 1. MODEL WITHOUT COVARIATES:
esttab first rf iv, keep(`dep' `iv') ///
	mtitles("First Stage" "Reduced Form" "TSLS") ///
	b(%6.4fc) se(%6.4fc) star(* 0.1 ** 0.05 *** 0.01) ///
	stats(N, fmt(%8.0fc) labels(Observations)) brackets ///
	coeflabels(s_net "Endogenous" z_snet "Instrument")

cap{
********************************************************************************
*ROW 2. INDIVIDUAL COVARIATES:
eststo clear

*1. FIRST STAGE:
local dep s_net
qui reg `dep' $xi `iv', cluster(rbd)
	eststo first

*2. REDUCED FORM:
qui reg sej2012 $xi `iv', cluster(rbd)
		eststo rf

*3. SECOND STAGE:
qui ivreg sej2012 $xi (`dep' = `iv'), cluster(rbd)
	eststo iv
}
*ROW 2. INDIVIDUAL COVARIATES:
esttab first rf iv, keep(z_snet s_net) ///
	mtitles("First Stage" "Reduced Form" "TSLS") ///
	b(%6.4fc) se(%6.4fc) star(* 0.1 ** 0.05 *** 0.01) ///
	stats(N, fmt(%8.0fc) labels(Observations)) brackets ///
	coeflabels(s_net "Endogenous" z_snet "Instrument")

cap{
********************************************************************************
*ROW 3. INDIVIDUAL & GROUP COVARIATES (BASELINE):
eststo clear

*1. FIRST STAGE:
qui reg `dep' $xi $xg `iv', cluster(rbd)
	eststo first

*2. REDUCED FORM:
qui reg sej2012 $xi $xg `iv', cluster(rbd)
	eststo rf

*3. SECOND STAGE:
qui ivreg sej2012 $xi $xg (`dep' = `iv'), cluster(rbd)
	eststo iv
}
*ROW 3. INDIVIDUAL & GROUP COVARIATES (BASELINE):
esttab first rf iv, keep(z_snet s_net) ///
	mtitles("First Stage" "Reduced Form" "TSLS") ///
	b(%6.4fc) se(%6.4fc) star(* 0.1 ** 0.05 *** 0.01) ///
	stats(N, fmt(%8.0fc) labels(Observations)) brackets ///
	coeflabels(s_net "Endogenous" z_snet "Instrument")
clear

********************************************************************************
********************************************************************************
*AGE IV, COWORKERS
*TABLE 2, PANEL B: ROBUSTNESS CHECKS
cap{
use "$data/base AGE IV.dta", clear

global xi edad edad2 sexo puntaje lnw anos_educ
global xg m_l_sexo m_l_puntaje m_l_anos_educ m_l_lnw

cd "$results"
local dep lnet
local iv zlnet
********************************************************************************
*ROW 1. MODEL WITHOUT COVARIATES:
eststo clear
*1. FIRST STAGE:
qui reg `dep' `iv', cluster(rut_emp)
	eststo first_firm

*2. REDUCED FORM:
qui reg sej2012 `iv', cluster(rut_emp)
	eststo rf_firm

*3. SECOND STAGE:
qui ivreg sej2012 (`dep' = `iv'), cluster(rut_emp)
	eststo iv_firm
	}
*ROW 1. MODEL WITHOUT COVARIATES:
esttab first_firm rf_firm iv_firm, ///
	b(%6.4fc) se(%6.4fc) star(* 0.1 ** 0.05 *** 0.01) ///
	stats(N, fmt(%8.0fc) labels(Observations))  ///
	brackets coeflabels(`dep' "Endogenous" `iv' "Instrument") ///
	keep(`dep' `iv') mtitles("First Stage" "Reduced Form" "TSLS")

cap{
********************************************************************************
*ROW 2. INDIVIDUAL COVARIATES:
eststo clear
*1. FIRST STAGE:
qui reg lnet zlnet $xi, cluster(rut_emp)
	eststo first_firm

*2. REDUCED FORM:
qui reg sej2012 zlnet $xi, cluster(rut_emp)
	eststo rf_firm

*3. SECOND STAGE:
qui ivreg sej2012 $xi (lnet = zlnet), cluster(rut_emp)
	eststo iv_firm
}
*ROW 2. INDIVIDUAL COVARIATES:
esttab first_firm rf_firm iv_firm, ///
	b(%6.4fc) se(%6.4fc) star(* 0.1 ** 0.05 *** 0.01) ///
	stats(N, fmt(%8.0fc) labels(Observations))  ///
	brackets coeflabels(`dep' "Endogenous" `iv' "Instrument") ///
	keep(`dep' `iv') mtitles("First Stage" "Reduced Form" "TSLS")
	
cap{
********************************************************************************
*ROW 3. INDIVIDUAL & GROUP COVARIATES (BASELINE):

*1. FIRST STAGE:
qui reg `dep' $xi $xg `iv', cluster(rut_emp)
	eststo first_firm

*2. REDUCED FORM:
qui reg sej2012 $xi $xg `iv', cluster(rut_emp)
	eststo rf_firm

*3. SECOND STAGE:
qui ivreg sej2012 $xi $xg (`dep' = `iv'), cluster(rut_emp)
	eststo iv_firm
}
*ROW 3. INDIVIDUAL & GROUP COVARIATES (BASELINE):
esttab first_firm rf_firm iv_firm, ///
	b(%6.4fc) se(%6.4fc) star(* 0.1 ** 0.05 *** 0.01) ///
	stats(N, fmt(%8.0fc) labels(Observations))  ///
	brackets coeflabels(`dep' "Endogenous" `iv' "Instrument") ///
	keep(`dep' `iv') mtitles("First Stage" "Reduced Form" "TSLS")
clear
